/*
 * Copyright (C) 2014 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okhttp3

import java.io.IOException
import okio.Timeout

/**
 * 一个Call是一个已经准备好执行的Request，一个Call可以被取消。由于该类表示单个请求/响应对，所以，不能执行两次。
 * A call is a request that has been prepared for execution. A call can be canceled. As this object
 * represents a single request/response pair (stream), it cannot be executed twice.
 */
interface Call : Cloneable {
  /** Returns the original request that initiated this call. */
  fun request(): Request

  /**
   * 立即调用 request ，阻塞当前队列，直至 response 可用或者出错。
   * Invokes the request immediately, and blocks until the response can be processed or is in error.
   *
   * To avoid leaking resources callers should close the [Response] which in turn will close the
   * underlying [ResponseBody].
   *
   * ```java
   * // ensure the response (and underlying response body) is closed
   * try (Response response = client.newCall(request).execute()) {
   *   ...
   * }
   * ```
   *
   * Caller 可以通过response的 [Response.body] 方法读取response的body。为了避免资源泄露，Caller必须关闭
   * response 的body。
   * The caller may read the response body with the response's [Response.body] method. To avoid
   * leaking resources callers must [close the response body][ResponseBody] or the response.
   *
   * <p>需要注意的是传送层成功（接收一个HTTP响应代码，标头和身体）并不一定表示应用层的成功：
   * response 可能仍表示相同的404或500不愉快的HTTP响应代码。
   * Note that transport-layer success (receiving a HTTP response code, headers and body) does not
   * necessarily indicate application-layer success: `response` may still indicate an unhappy HTTP
   * response code like 404 or 500.
   *
   * @throws IOException if the request could not be executed due to cancellation, a connectivity
   *     problem or timeout. Because networks can fail during an exchange, it is possible that the
   *     remote server accepted the request before the failure.如果因为该请求因为被取消、连接失败、超时等原因而不能被执行，就抛出IOException。
   *     因为网络可能在任何一次数据交换时失败，所以远程服务可能已经在客户端得到失败结果前接受了这次请求。
   * @throws IllegalStateException when the call has already been executed. 如果这个Call已经被执行过了，就抛出IllegalStateException
   */
  @Throws(IOException::class)
  fun execute(): Response

  /**
   * Schedules the request to be executed at some point in the future.
   * 安排请求在未来的某个时候被执行。
   *
   * The [dispatcher][OkHttpClient.dispatcher] defines when the request will run: usually
   * immediately unless there are several other requests currently being executed.
   *
   * <p>{@link OkHttpClient#getDispatcher dispatcher} 决定该请求将在合适被执行。
   * 如果没有其他几个请求正在被执行，通常会立即被执行。
   *
   * This client will later call back `responseCallback` with either an HTTP response or a failure
   * exception.
   * <p>客户端稍后会得到一个HTTP请求成功的response回调，或者是一个失败的异常。如果你在完成之前取消了该请求，
   * 那么回调不会被调用。
   *
   * @throws IllegalStateException when the call has already been executed. 如果这个Call已经被执行过了，就抛出IllegalStateException
   */
  fun enqueue(responseCallback: Callback)

  /**
   * Cancels the request, if possible. Requests that are already complete cannot be canceled.
   * 尝试取消请求。已经完成的请求无法被取消掉。
   * */
  fun cancel()

  /**
   * Returns true if this call has been either [executed][execute] or [enqueued][enqueue]. It is an
   * error to execute a call more than once.AsyncCall
   */
  fun isExecuted(): Boolean

  fun isCanceled(): Boolean

  /**
   * Returns a timeout that spans the entire call: resolving DNS, connecting, writing the request
   * body, server processing, and reading the response body. If the call requires redirects or
   * retries all must complete within one timeout period.
   *
   * Configure the client's default timeout with [OkHttpClient.Builder.callTimeout].
   */
  fun timeout(): Timeout

  /**
   * Create a new, identical call to this one which can be enqueued or executed even if this call
   * has already been.
   */
  public override fun clone(): Call

  fun interface Factory {
    fun newCall(request: Request): Call
  }
}
